﻿//  Created by Mathew Purchase.
//  Copyright (c) 2014 Mathew Purchase. All rights reserved.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class LightService : MonoBehaviour 
{

	//---------------------------------------------------------------------------------------------
	// Properties
	public int currentRotation;
	public int currentColour;
	
	
	//---------------------------------------------------------------------------------------------
	// Members
	
	public Animator m_animator;
	public List<Color> LightColors;
	public Light Daylight;
	public List<Light> CityLights;
	
	
	//---------------------------------------------------------------------------------------------
	// Events and Delegates
	
	
	//---------------------------------------------------------------------------------------------
	// Init
	
	void OnEnable () 
	{
		TurnService.turnEnd += ChangeDaylight;
		terrainGeneration.ready += SetCityLights;
	}
	
	void OnDisable ()
	{
		TurnService.turnEnd -= ChangeDaylight;
	}
	
	
	//---------------------------------------------------------------------------------------------
	// Core Functions
	void ChangeCityLight()
	{
		if (currentRotation == 2)
		{
			for (int a = 0; a < CityLights.Count; a++)
			{
				CityLights[a].intensity = 0.5f;
			}
		}
		else
		{
			for (int a = 0; a < CityLights.Count; a++)
			{
				CityLights[a].intensity = 0f;
			}
		}
		
	
	}
	//---------------------------------------------------------------------------------------------


	void ChangeDaylight () 
	{
		currentRotation ++;
		if (currentRotation > 3)
		{
			currentRotation = 0;
		}
		
		m_animator.SetInteger("Position", currentRotation);
		Daylight.color = LightColors[currentRotation];
		ChangeCityLight();

	}
	//---------------------------------------------------------------------------------------------
	
	void SetCityLights()
	{
		GameObject[] foundlights = GameObject.FindGameObjectsWithTag("CityLight");
		
		for (int a = 0; a < foundlights.Length; a++)
		{
			CityLights.Add(foundlights[a].GetComponent<Light>());
		}
	}
}
